<?php

#LOADING PAGES
include "../../site_specific/project.php";
include "../../includes/constants.php";
include "../../site_specific/external_constants.php";
include "../../includes/phpmailer/class.phpmailer.php";
include "../../includes/phpmailer/class.smtp.php";
include "../../includes/utilities.php";
include "../../includes/pages.php";
include "../../includes/database.php";
include "../../includes/users.php";
include "../../includes/date.php";
include "../../includes/text.php";
include "../../includes/files.php";

# Establish database connection
establishDatabaseConnection();

$sql_all_parents = "SELECT distinct p_parent FROM ".$PROJECT_DB_TABLES["pages"];

$current_data_all_parents = getRecord($sql_all_parents);

$sql_module_level = "SELECT p_id FROM ".$PROJECT_DB_TABLES["pages"]." WHERE ";

$count = 0;
foreach($current_data_all_parents as $key_all_parents=>$value_all_parents){
	
	$sql_module_level.="p_id!=\"".$value_all_parents["p_parent"]."\"";
	
	if(count($current_data_all_parents)!=$count+1){
		
		$sql_module_level.=" AND ";
	}
	
	$count++;
}

$current_data_module_level = getRecord($sql_module_level);

#Get the parent
$parent = $_GET["id"];

$topflag = $_GET["top"];

$moveflag = $_GET["move"];

#select kids
$sql_get_kids = "";

if($topflag=="true"){
	$sql_get_kids = "SELECT ".$PROJECT_DB_TABLES["pages"].".* FROM ".$PROJECT_DB_TABLES["pages"]." WHERE p_section=\"".$parent."\" AND p_parent=\"\" AND p_status>0 AND p_status<3 ORDER BY p_sort ASC";
}
else{
	$sql_get_kids = "SELECT ".$PROJECT_DB_TABLES["pages"].".* FROM ".$PROJECT_DB_TABLES["pages"]." WHERE p_parent=\"".$parent."\" AND p_status>0 AND p_status<3 ORDER BY p_sort ASC";	
}

#Get data		
$current_data_kids = getRecord($sql_get_kids);

$max_sort = 0;

#This function is to check if all the lower level page has sort
if($moveflag=="true"){
	
	foreach($current_data_kids as $current_value_tmp){
		
		if(!is_null($current_value["p_sort"]) && !empty($current_value["p_sort"])){
			
			if($current_value["p_sort"] > $max_sort){
				
				$max_sort = $current_value["p_sort"];
			}
		}
	}
}

$output .= "<table cellspacing=\"0\" align=\"left\">";

foreach($current_data_kids as $current_key=>$current_value){

	$module_flag=false;
	foreach($current_data_module_level as $value_current_data_module_level){
		if($current_value["p_id"]==$value_current_data_module_level["p_id"]){
			$module_flag=true;
			break;
		}
	}
	
	if($moveflag=="true"){
		
		if(is_null($current_value["p_sort"])){
			
			$max_sort = $max_sort + 1;
			
			$update_sql = "UPDATE ".$PROJECT_DB_TABLES["pages"]." SET p_sort=".$max_sort." WHERE p_id=\"".$current_value["p_id"]."\"";
			
			echo $update_sql;
			
			executeSQLQuery($update_sql);
		}
		
		#this html code is for the move page
		if($module_flag){
		#If the item don`t have kids, the icon will be document.png
			$output.="<tr><td valign=\"top\" style=\"border-color: white;border-style: solid;\"><img id=\""."img_".$current_value["p_id"]."\" src=\"../assets/images/tree/document.png\");\"/></td><td style=\"border-color: white;border-style: solid;\" align=\"left\">";
			$output.="<a id=\"a_".$current_value["p_id"]."\" onclick=\"\" style=\"text-decoration:none;\" href=\"javascript:itemSelected('".$current_value["p_id"]."','".$current_value["p_parent"]."','".$current_value["p_section"]."');\">".$current_value["p_name"]."(".$current_value["p_id"].")"."</a><br/><div align=\"left\" id=\""."div".$current_value["p_id"]."\"></div></td></tr>";
			
		}
		
		else{	
		#If the item have kids, the icon will be folder_closed.png
			$output.="<tr><td valign=\"top\" style=\"border-color: white;border-style: solid;\"><img id=\""."img_".$current_value["p_id"]."\" src=\"../assets/images/tree/folder_closed.png\" onclick=\"ExpandPage('".$current_value["p_id"]."',false,true);\"/></td><td style=\"border-color: white;border-style: solid;\" align=\"left\">";
			$output.="<a id=\"a_".$current_value["p_id"]."\" onclick=\"\" style=\"text-decoration:none;\" href=\"javascript:itemSelected('".$current_value["p_id"]."','".$current_value["p_parent"]."','".$current_value["p_section"]."');\">".$current_value["p_name"]."(".$current_value["p_id"].")"."</a><br/><div align=\"left\" id=\""."div".$current_value["p_id"]."\"></div></td></tr>";
			
		}
	}
	else{
		#these html codes is for the page selection
		if($module_flag){
		#If the item don`t have kids, the icon will be document.png
			$output.="<tr><td valign=\"top\" style=\"border-color: white;border-style: solid;\"><img id=\""."img_".$current_value["p_id"]."\" src=\"../assets/images/tree/document.png\");\"/></td><td style=\"border-color: white;border-style: solid;\" align=\"left\">";
			$output.="<a id=\"a_".$current_value["p_id"]."\" onclick=\"\" style=\"text-decoration:none;\" href=\"javascript:foo('".$current_value["p_id"]."','".$current_value["p_section"]."');\">".$current_value["p_name"]."(".$current_value["p_id"].")"."</a><br/><div align=\"left\" id=\""."div".$current_value["p_id"]."\"></div></td></tr>";
			
		}
		
		else{	
		#If the item have kids, the icon will be folder_closed.png
			$output.="<tr><td valign=\"top\" style=\"border-color: white;border-style: solid;\"><img id=\""."img_".$current_value["p_id"]."\" src=\"../assets/images/tree/folder_closed.png\" onclick=\"ExpandPage('".$current_value["p_id"]."',false,false);\"/></td><td style=\"border-color: white;border-style: solid;\" align=\"left\">";
			$output.="<a id=\"a_".$current_value["p_id"]."\" onclick=\"\" style=\"text-decoration:none;\" href=\"javascript:foo('".$current_value["p_id"]."','".$current_value["p_section"]."');\">".$current_value["p_name"]."(".$current_value["p_id"].")"."</a><br/><div align=\"left\" id=\""."div".$current_value["p_id"]."\"></div></td></tr>";
			
		}
	}
}

$output .= "</table>";
//header('Access-Control-Allow-Origin: *');
echo $output;

?>